﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ConcordWinManager
{
    public partial class ConsultantForm : Form
    {
        public ConsultantForm()
        {
            InitializeComponent();
        }

        Consultant consultant = new Consultant();

        private void ConsultantForm_Load(object sender, EventArgs e)
        {
            btnAdd.Visible = Settings.consultant.user.IsAdminRole;
            btnChange.Visible = Settings.consultant.user.IsAdminRole;

            UpdateForm();
        }
        
        private void btnAdd_Click(object sender, EventArgs e)
        {
            ConsultantEditForm consultantEditForm = new ConsultantEditForm(this, "Добавление");
            consultantEditForm.ShowDialog();
            UpdateForm();
        }

        private void btnChange_Click(object sender, EventArgs e)
        {
            ConsultantEditForm consultantEditForm = new ConsultantEditForm(this, "Изменение",
                (int)dataGridViewTable.Rows[dataGridViewTable.SelectedCells[0].RowIndex].Cells["id_consultant"].Value);

            consultantEditForm.ShowDialog();
            UpdateForm();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            UpdateForm();
        }

        public void UpdateForm()
        {
            DataSet ds = new DataSet();
            Tools.RunWithProgressIndicator(this, delegate { consultant.SelectAll(ref ds); });

            dataGridViewTable.DataSource = ds.Tables[0];
            dataGridViewTable.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridViewTable.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            dataGridViewTable.Columns["id_consultant"].Visible = false;
            dataGridViewTable.Columns["id_rezident"].Visible = false;
            dataGridViewTable.Columns["id_address"].Visible = false;

            dataGridViewTable.Refresh();
        }

        private void btnChangeItem_Click(object sender, EventArgs e)
        {

        }

        private void btnBlock_Click(object sender, EventArgs e)
        {

        }
    }
}
